﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Win32;

namespace OmBleTool
{
    public partial class Form1 : Form
    {

        public  Point macPoint { get { return new Point(352, 252); } }
        public  Point burnPoint { get { return new Point(614, 539); } }
        public Form1()
        {
            InitializeComponent();
            //timer1.Start();

        }
        private void GetMousePoint()
        {
            Point ms = Control.MousePosition;
            this.label2.Text = string.Format("当前鼠标位置：{0}:{1}", ms.X, ms.Y);
            MouseButtons mb = Control.MouseButtons;

        }
        //更新鼠标位置等信息。
        private void timer1_Tick(object sender, EventArgs e)
        {
            GetMousePoint();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            bool open= Win32Helper.GetAPP(panel1);
            if (open==false)
            {
                this.Close();
            }
            //this.Location = new Point(0, 0);
        }


        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    string[] code = textBox1.Text.Trim().Split('-');
                    if (code.Length != 2)
                    {
                        MessageBox.Show("条码错误!");
                        this.Activate();
                        this.Focus();
                        textBox1.Focus();
                        textBox1.SelectAll();
                        return;
                    }
                    string mac = code[0].Substring(code[0].Length - 12, 12);
                    mac = mac.Substring(10, 2) + ":"
                        + mac.Substring(8, 2) + ":"
                        + mac.Substring(6, 2) + ":"
                        + mac.Substring(4, 2) + ":"
                        + mac.Substring(2, 2) + ":"
                        + mac.Substring(0, 2);
                    Point point1 = new Point(macPoint.X + Location.X, macPoint.Y + Location.Y);
                    Win32Helper.MouseClick(point1);
                    Win32Helper.MouseClick(point1);
                    Clipboard.SetText(mac);
                    Thread.Sleep(500);
                    Win32Helper.KeyDown(Keys.ControlKey, Keys.V);
                    Thread.Sleep(500);
                    Win32Helper.KeyDown(Keys.Enter);
                    Thread.Sleep(500);
                    Point point2 = new Point(burnPoint.X + Location.X, burnPoint.Y + Location.Y);
                    Win32Helper.MouseClick(point2);
                    textBox1.Clear();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    this.Activate();
                    this.Focus();
                    textBox1.Focus();
                    textBox1.SelectAll();
                    return;
                }
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Process[] myproc = Process.GetProcesses();
            foreach (Process item in myproc)
            {
                if (item.ProcessName.Contains("OmBleConfigurator"))
                {
                    item.Kill();
                    Thread.Sleep(100);
                }
            }
            System.Environment.Exit(0);
        }
    }
}
